home *** CD-ROM | disk | FTP | other *** search
/ Compendium Deluxe 2 / LSD and 17bit Compendium Deluxe - Volume II.iso / a / prog / cprog / shade2obj.lha / shading.c < prev   
Encoding:
C/C++ Source or Header  |  1993-02-08  |  3.2 KB  |  121 lines

  1. /********************************************************/
  2. /* This is the source code for the simple SFS algorithm */
  3. /* It will read the UCF format image, and output the    */
  4. /* estimated depth maps as a raw data file.             */
  5. /* It can be complied as follow:            */
  6. /*      cc -g -o shading shading.c -lm            */
  7. /********************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <math.h>
  11.  
  12. #define Size 128        /* image size 128 x 128 */
  13. #define MAX(a,b)        (((a) < (b)) ? (b) : (a))
  14.  
  15. typedef unsigned char BYTE;
  16.  
  17. typedef struct
  18. {
  19.         int type;
  20.         unsigned int    maxX,
  21.                         maxY;
  22.         unsigned char   *image;
  23. }       PIC;
  24.  
  25. PIC UCFReadPic(infile)
  26. FILE *infile;
  27. {
  28.    PIC temp;
  29.  
  30.    /* getting Type from image data */
  31.    fread(&temp.type,sizeof(temp.type),1,infile);
  32.    switch (temp.type)
  33.    {
  34.       case 0xF10F:
  35.       case 0xF200:
  36.       case 0xF201:
  37.       case 0xF204:
  38.       case 0x0000:
  39.       {  
  40.          fread(&temp.maxX,sizeof(temp.maxX),1,infile);
  41.          fread(&temp.maxY,sizeof(temp.maxY),1,infile);
  42.          break;
  43.       }
  44.       case 0x8000:
  45.       case 0x8001:
  46.       case 0xB003:
  47.       default  :
  48.       {
  49.          fread(&temp.maxX,sizeof(temp.maxX),1,infile);
  50.          fread(&temp.maxY,sizeof(temp.maxY),1,infile);
  51.          break;
  52.       }
  53.    }
  54.    if((temp.image=(BYTE*)calloc(temp.maxX*temp.maxY,sizeof(BYTE)))==NULL)
  55.    {
  56.       temp.maxX = temp.maxY = 0;
  57.       temp.image = NULL;
  58.       return(temp);
  59.    }
  60.  
  61.    fread(temp.image,sizeof(BYTE),temp.maxX * temp.maxY,infile);
  62.    return(temp);
  63. }
  64.  
  65. main()
  66. {
  67.  char   filename[80];
  68.  FILE   *outfile,*infile;
  69.  int i,j,I,iter;
  70.  float Ps,Qs,p,q,pq,PQs,fZ,dfZ,Eij,Wn=0.0001*0.0001,Y,K;
  71.  float Zn[Size][Size],Zn1[Size][Size],Si1[Size][Size],Si[Size][Size];
  72.  PIC pic1;
  73.  
  74. /* assume the initial estimate zero at time n-1 */
  75.  for(i=0;i<Size;i++)
  76.   for(j=0;j<Size;j++){
  77.    Zn1[i][j] = 0.0;
  78.    Si1[i][j] = 0.01; }
  79.  printf("Input number of iterations : ");
  80.  scanf("%d",&iter);
  81.  printf("\nInput the image filename : ");
  82.  scanf("%s",filename);
  83.  infile = fopen(filename,"r");
  84.  pic1 = UCFReadPic(infile);
  85.  printf("\nInput the light source direction : ");
  86.  printf("\nPs = ");
  87.  scanf("%f",&Ps);
  88.  printf("\nQs = ");
  89.  scanf("%f",&Qs);
  90.  
  91. /************************************************************************/
  92.  for(I=1;I<=iter;I++){
  93.   for(i=0;i<Size;i++)
  94.    for(j=0;j<Size;j++){ /* calculate -f(Zij) & df(Zij) */
  95.     if(j-1 < 0 || i-1 < 0) /* take care boundary */
  96.       p = q = 0.0;
  97.     else {
  98.           p = Zn1[i][j] - Zn1[i][(j-1)];
  99.           q = Zn1[i][j] - Zn1[i-1][j]; }
  100.     pq = 1.0 + p*p + q*q;
  101.     PQs = 1.0 + Ps*Ps + Qs*Qs;
  102.     Eij = pic1.image[i*pic1.maxX+j]/255.0;
  103.     fZ = -1.0*(Eij - MAX(0.0,(1+p*Ps+q*Qs)/(sqrt(pq)*sqrt(PQs))));
  104.     dfZ = -1.0*((Ps+Qs)/(sqrt(pq)*sqrt(PQs))-(p+q)*(1.0+p*Ps+q*Qs)/
  105.                        (sqrt(pq*pq*pq)*sqrt(PQs))) ;
  106.     Y = fZ + dfZ*Zn1[i][j];
  107.     K = Si1[i][j]*dfZ/(Wn+dfZ*Si1[i][j]*dfZ);
  108.     Si[i][j] = (1.0 - K*dfZ)*Si1[i][j]; 
  109.     Zn[i][j] = Zn1[i][j] + K*(Y-dfZ*Zn1[i][j]);}
  110.  
  111.   printf("\nOutput depth map !\n");
  112.   sprintf(filename,"tmap%d.out",I);
  113.   outfile = fopen(filename,"w");
  114.   for(i=0;i<Size;i++)
  115.    for(j=0;j<Size;j++){
  116.     fprintf(outfile,"%f\n",Zn[i][j]);
  117.     Zn1[i][j] = Zn[i][j];
  118.     Si1[i][j] = Si[i][j];}
  119.   fclose(outfile); }
  120. } /* end of main */
  121.